﻿using Infrastructure.Excel;
using Infrastructure.Web;
using MyData.Models;
using MyWeb.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyWeb.Controllers
{
    public class CarRecordController : BaseController
    {
        // GET: CarRecord
        [HandlerLogin]
        public ActionResult CarRecordIndex()
        {
            return View();
        }
        public ActionResult GetCarRecord(QueryListModel query, string Carplant,string StartDate,string EndDate)
        {
            DateTime startDate = DateTime.Parse(StartDate).Date;
            DateTime endDate = DateTime.Parse(EndDate).Date.AddDays(1);
            var result = CarRecordApp.GetCarRecord(query, Carplant,startDate,endDate);
            return Json(result, JsonRequestBehavior.AllowGet);


        }
        public FileResult ExcelImport()
        {
            List<CarRecord> checkInDtos = Session["Data"] as List<CarRecord>;
            if (checkInDtos == null) checkInDtos = new List<CarRecord>();
            string filename = $"~/Export/{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            filename = Server.MapPath(filename);
            EpplusHelper.SaveToExcel(checkInDtos, filename);
            return File(filename, "application/octet-stream", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xls");

        }
    }
}